import fs from 'fs';
import glob from 'glob';

/**
 *
 *
 * @export
 * @param {string} [date] - 日期 `20230715-20230722`
 * @return
 */
export async function importJson<T>(path: string, date?: string): Promise<T[]> {
  let files = glob.sync(process.cwd() + path + '/*.json');

  if (date) {
    files = files.filter((v) => v.includes(date));
  }

  if (!files.length) throw new Error('没有找到数据');

  const data: T[][] = [];
  for (const file of files) {
    const json = fs.readFileSync(file).toString();
    data.push(JSON.parse(json) as T[]);
  }

  return data.flat();
}
